package com.headwind.logistics.logistics.controller;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrder;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrderContent;
import com.headwind.logistics.logistics.service.intl.LogisticsCustomerServiceIntl;
import com.headwind.logistics.logistics.service.intl.LogisticsWorkOrderServiceIntl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 小恶魔
 * @Date 2020-08-01 9:43
 * @Version 1.0
 * @DESCRIPTION 顾客工单控制层
 */
@Controller
@RequestMapping("/logistics/customer")
public class CustomerWorkOrderController {

    //前台客户工单业务逻辑层
    @Autowired
    private LogisticsWorkOrderServiceIntl logisticsWorkOrderServiceIntl;

    //api响应接口
    @Autowired
    private ResultGenerator resultGenerator;

    /**
     * 根据顾客获取工单信息
     * @param session
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("queryWorkOrder")
    @ResponseBody
    public RestResult queryWorkOrder(HttpSession session,Integer page,Integer rows,String startDate,String endDate){
        Map<String,Object> map = new HashMap<>();
        //从session中获取顾客信息
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        List<LogisticsWorkOrder> logisticsWorkOrders =
                logisticsWorkOrderServiceIntl.queryWorkOrderByCustomerId(startDate,endDate,customer.getId(), page, rows);

        map.put("pageCount",logisticsWorkOrderServiceIntl.queryWorkOrderByCustomerId(startDate,endDate,customer.getId(), null, null).size());
        map.put("data",logisticsWorkOrders);

        return resultGenerator.getSuccessResult(map);
    }

    /**
     * 添加工单内容
     * @param workOrderContent
     * @param session
     * @return
     */
    @PostMapping("addWorkOrderContent")
    @ResponseBody
    public RestResult addWorkOrderContent(LogisticsWorkOrderContent workOrderContent,HttpSession session){
        //从session获取顾客对象
        Customer customer = (Customer) session.getAttribute(String.valueOf(UserSessionKey.CUSTOMER));

        workOrderContent.setCustomerId(customer.getId());
        Integer count = logisticsWorkOrderServiceIntl.addWorkOrderContent(workOrderContent);

        if (count > 0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("回复失败哦，重新提交试试");
    }

    /**
     * 修改工单信息
     * @param workOrder
     * @return
     */
    @PostMapping("updateWorkOrder")
    @ResponseBody
    public RestResult updateWorkOrderById(LogisticsWorkOrder workOrder){

        Integer count = logisticsWorkOrderServiceIntl.updateWorkOrder(workOrder);

        if (count > 0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("完成工单失败，重新试试看?");
    }

}
